package com.cuotoniu.lazybug.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;


/**
 * Properties tools.
 * 
 * @author HLJ
 *
 */
public class PropertiesUtil {

	private static Path path;	/** Path Object,use to sure the file root path */
	private static String filepath;	/** file path */
	
	/**
	 * initialization file path(root path).
	 */
	public static void initialization(){
		if(path == null) 
			path = new Path();
		filepath = path.getClassRootPath();
	}
	
	/**
	 * initialization file path.
	 * 
	 * @param file
	 */
	public static void initialization(String file){
		if(path == null) 
			path = new Path();
		filepath = path.getClassRootPath() + file;
	}
	
	/**
	 * set the file path,include the root path.
	 * 
	 * @param file
	 */
	public static void setPropertyFile(String file){
		filepath = filepath + file;
	}
	
	/**
	 * create the Properties.
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Properties getProperties() throws Exception{
		FileInputStream in = new FileInputStream(new File(filepath));
		Properties p = new Properties();
		p.load(in);
		return p;
	}
	
	/**
	 * create the Properties.
	 * 
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static Properties getProperties(String file) throws Exception{
		FileInputStream in = new FileInputStream(new File(filepath+file));
		Properties p = new Properties();
		p.load(in);
		return p;
	}
	
	/**
	 * create the Properties.
	 * 
	 * @param fullpath
	 * @return
	 * @throws Exception
	 */
	public static Properties getPropertiesByFullPath(String fullpath) throws Exception{
		FileInputStream in = new FileInputStream(new File(fullpath));
		Properties p = new Properties();
		p.load(in);
		return p;
	}
	
	/**
	 * get the value by the key.
	 * if the value is null or empty,set the value with defaultVal.
	 * 
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static String getValue(String key,String defaultVal){
		try {
			return getProperties().getProperty(key,defaultVal).trim();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * get the value by the key.
	 * 
	 * @param key
	 * @return
	 */
	public static String getValue(String key){
		try {
			return getProperties().getProperty(key).trim();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * get the value by the key with the relative file path.
	 * if the value is null or empty,set the value with defaultVal.
	 * 
	 * @param file
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static String getValueByFile(String file,String key,String defaultVal){
		try {
			return getProperties(file).getProperty(key,defaultVal).trim();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * get the value by the key with the relative file path.
	 * 
	 * @param file
	 * @param key
	 * @return
	 */
	public static String getValueByFile(String file,String key){
		try {
			return getProperties(file).getProperty(key).trim();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * get the value by the key with the absolute file path.
	 * 
	 * @param file
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public static String getValueByFullPath(String file,String key){
		try {
			return getPropertiesByFullPath(file).getProperty(key).trim();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * get the value by the key with the absolute file path.
	 * if the value is null or empty,set the value with defaultVal.
	 * 
	 * @param file
	 * @param key
	 * @return
	 */
	public static String getValueByFullPath(String file,String key,String defaultVal){
		try {
			return getPropertiesByFullPath(file).getProperty(key).trim();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * out the property content to the file.
	 * 
	 * @param p
	 * @param path
	 */
	public static void writeByProperties(Properties p,String path){
		try {
			p.list(new PrintStream(new File(path)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * according to the map data,it will output the property content to the file.
	 * the path:relative
	 * 
	 * @param path
	 * @param map
	 */
	public static void writeByRelativePath(String path,Map<String, String> map){
		try {
			Properties p = new Properties();
			for (Entry<String, String> m : map.entrySet()) {
				p.setProperty(m.getKey(), m.getValue());
			}
			writeByProperties(p,(filepath + path));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * according to the map data,it will output the property content to the file.
	 * the path:absolute
	 * 
	 * @param path
	 * @param map
	 */
	public static void writeByFullPath(String full,Map<String, String> map){
		try {
			Properties p = new Properties();
			for (Entry<String, String> m : map.entrySet()) {
				p.setProperty(m.getKey(), m.getValue());
			}
			writeByProperties(p,(full));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
